﻿//Public Domain
//Original version written by Paul Aspinall.

using System;
using System.Drawing;

using SdlDotNet.Core;

using SdlDotNet.Input;
using SdlDotNet.Graphics;
using SdlDotNet.Graphics.Sprites;

using SdlDotNet.Audio;

using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

namespace bloodbox
{
    /// <summary>
    /// 
    /// </summary>
    public class Program
    {

        private Surface Screen;             // video screen
        private int ScreenWidth  = 1024;    // screen width
        private int ScreenHeight = 768;     // screen height
        private int MapPlayed    = 1;       // id of the map currently player

        private static SoundDictionary soundBox = new SoundDictionary();
        private static MusicDictionary musicBox = new MusicDictionary();
        private static MapDrawer map;
        private static BBUI gui;
        private static BBConsole console;

        public static void Main()
        {
            Run();
        }
        
        /// <summary>Main thread</summary>
        [STAThread]
        public static void Run()
        {
            Program game = new Program();
            game.Go();
        }

        /// <summary>
        /// 
        /// </summary>
        public Program()
        {
            Screen = Video.SetVideoMode(ScreenWidth, ScreenHeight);

            map     = new MapDrawer(Screen);
            console = new BBConsole(Screen);
            gui     = new BBUI(Screen, console);
            TextDrawer.Initialize(Screen);
            
            // Data loading

            // Sounds initialization
            SoundHandler soundHandler = new SoundHandler();

            soundBox = soundHandler.initializeSounds();
            Console.WriteLine("Sounds Loading ");

            musicBox = soundHandler.initializeMusics();
            Console.WriteLine("Musics Loading");

            //MusicBox["dtowne"].Play();

            map.loadMap(MapPlayed); // Loading of the map
            Console.WriteLine("Map Loading");

            Video.WindowIcon();
            Video.WindowCaption = "BloodBox ! It roxes so much !";
            
            this.AddHandlers();
            Events.Run();
        }

        /// <summary>
        /// 
        /// </summary>
        public void Go()
        {
            Events.KeyboardDown += new EventHandler<KeyboardEventArgs>(this.KeyboardDown);
            Events.MouseButtonDown += new EventHandler<MouseButtonEventArgs>(this.OnMouseButtonDown);
            Events.Tick += new EventHandler<TickEventArgs>(Events_Tick);
            Events.Quit += new EventHandler<QuitEventArgs>(Events_Quit);
            Events.Run();
            
        }

        // This changes the rendering of an OpenGL heightmap when the mouse button is pressed.
        private void OnMouseButtonDown(object sender, MouseButtonEventArgs e)
        {
            map.MoveMap(e.X, e.Y);
            Console.WriteLine("x = "+ e.X + ", y = "+e.Y);
        }
        
        private void AddHandlers()
        {
            Events.KeyboardDown += new EventHandler<KeyboardEventArgs>(this.KeyboardDown);
            Events.MouseButtonDown += new EventHandler<MouseButtonEventArgs>(this.OnMouseButtonDown);
            Events.Quit += new EventHandler<QuitEventArgs>(this.Events_Quit);
            Events.Tick += new EventHandler<TickEventArgs>(this.Events_Tick);
        }

        private void RemoveHandlers()
        {
            Events.KeyboardDown -= new EventHandler<KeyboardEventArgs>(this.KeyboardDown);
            Events.MouseButtonDown -= new EventHandler<MouseButtonEventArgs>(this.OnMouseButtonDown);
            Events.Quit -= new EventHandler<QuitEventArgs>(this.Events_Quit);
            Events.Tick -= new EventHandler<TickEventArgs>(this.Events_Tick);
        }
        
        private void Events_Tick(object sender, TickEventArgs e)
        {
            // Update everything
            gui.Update();

            // Clear the screen
            Screen.Fill(Color.Black);

            // Then draw everything
            map.drawMap();
            gui.Draw();

            TextDrawer.DrawText(new Point(250, 250), "plopeuuuuuu", TextDrawer.TextFonts.Standard, Color.White);

            // And finally update the screen
            Screen.Update();                   
        }

        private void Events_Quit(object sender, QuitEventArgs e)
        {
            Events.QuitApplication();
        }

        private void KeyboardDown(object sender, KeyboardEventArgs e)
        {
            // Check if the key pressed was a Q or Escape
            if (e.Key[Key.Escape])
            {
                Events.QuitApplication();
            } else
            {                
                console.KeyPushed(e.KeyboardCharacter, e.Mod);
            }
        }

        /// <summary>
        /// For purposes of the demo browser only.
        /// </summary>
        public static string Title
        {
            get
            {
                return "Template: Used as a very basic template for new SdlDotNet apps.";
            }
        }
    }
}